package cn.demo.service0.filter;

import cn.demo.service0.entity.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * @Author: ljh
 * @Date: 2019/10/10 22:13
 * 保证服务间调用可以传递头信息,不使用熔断器
 */
@Slf4j
public class UserContextInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
        HttpHeaders headers=httpRequest.getHeaders();
        log.info("service0传递head-{}",UserContextHolder.getContext().getCorrelationId());
        headers.add(UserContext.CORRELATION_ID,UserContextHolder.getContext().getCorrelationId());
        return execution.execute(httpRequest,bytes);
    }
}
